<cfcomponent extends="Controller" output="false">
	
	
	<cffunction name="index">
		<cfset teams = model("Team").findAll()>
	</cffunction>
	
	
	<cffunction name="display">		
   		<cfset team = model("Team").findByKey(params.key)>
		<cfset findCurrentTeamPlayers = team.teamPlayer(include="Player",order="TeamPlayer.Position")>
		<cfset currentTeamPlayerIds = ValueList(findCurrentTeamPlayers.PlayerID)>
		<cfset playersInAnotherTeam = model("TeamPlayer").findAll(select="PlayerID")> <!--- TODO: Needs refinement for seasonal teams --->
		<cfset playersInAnotherTeam = ValueList(playersInAnotherTeam.PlayerID)>
		<!---<cfset teamPlayerHighest = model("TeamPlayer").maximum(property="TeamPlayer.Position",where="TeamID='#params.key#'")>--->
    	
	    <cfif NOT IsObject(team)>
	        <cfset flashInsert(error="Team #params.key# was not found")>
	        <cfset redirectTo(action="index")>
	    </cfif>
				
		<cfset teamPlayer = model("TeamPlayer").new()>
		<cfset teamPlayer.teamID = team.teamID>

		<!--- 'Add Players' Dropdown --->
		<cfset ignorePlayers = playersInAnotherTeam & currentTeamPlayerIds>
		<cfset players = model("Players").findAll(where="PlayerID NOT IN (#ignorePlayers#)")>
		
	</cffunction>	
	
	<cffunction name="register">
		<cfset team = model("Team").new()>
	</cffunction>	
	
	<cffunction name="modify">			
    	<cfset team = model("Team").findByKey(params.key)>
    
        <cfif NOT IsObject(team)>
	        <cfset flashInsert(error="Team #params.key# was not found")>
			<cfset redirectTo(action="index")>
	    </cfif>		
	</cffunction>
		
	<cffunction name="addRemoveTeamSeason" hint="Adds/Removes Teams from a Season">
		<cfset team = model("Team").findByKey(params.TeamID)>
		<cfset team.SeasonID = params.SeasonID>
		<cfif team.save()>
			<cfset flashInsert(success="Added/Removed team to/from season successfully.")>
		<cfelse>
			<cfset flashInsert(error="There was an error adding/remove the team to/from the season.")>
		</cfif>
		<cfset redirectTo(back=true)>
	</cffunction>
	
	<!--- CRUD methods --->
	
	<cffunction name="create">
		<cfset team = model("Team").new(params.team)>
		
		<cfif team.save()>
			<cfset flashInsert(success="The team was created successfully.")>
            <cfset redirectTo(action="index")>
		<cfelse>
			<cfset flashInsert(error="There was an error creating the team.")>
			<cfset renderPage(action="register")>
		</cfif>
	</cffunction>
	
	<cffunction name="update">
		<cfset team = model("Team").findByKey(params.key)>
		
		<cfif team.update(params.team)>
			<cfset flashInsert(success="The team was updated successfully.")>	
            <cfset redirectTo(action="index")>
		<cfelse>
			<cfset flashInsert(error="There was an error updating the team.")>
			<cfset renderPage(action="modify")>
		</cfif>
	</cffunction>
	
	<cffunction name="delete">
		<cfset team = model("Team").findByKey(params.key)>
		
		<cfif team.delete()>
			<cfset flashInsert(success="The team was deleted successfully.")>	
            <cfset redirectTo(action="index")>
		<cfelse>
			<cfset flashInsert(error="There was an error deleting the team.")>
			<cfset redirectTo(action="index")>
		</cfif>
	</cffunction>
	
</cfcomponent>
